package com.opdar.athena.web.controllers;

import com.opdar.athena.entities.ErrorResult;
import com.opdar.athena.web.base.Result;
import com.opdar.athena.entities.ProjectEntity;
import com.opdar.athena.entities.UserEntity;
import com.opdar.athena.web.interceptors.AuthViewInterceptor;
import com.opdar.athena.web.service.ProjectService;
import com.opdar.athena.web.service.UserService;
import com.opdar.platform.annotations.Interceptor;
import com.opdar.platform.annotations.JSON;
import com.opdar.platform.annotations.Request;
import com.opdar.platform.core.base.Context;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;

/**
 * Created by shiju on 2017/6/20.
 */
@Controller
@Interceptor(AuthViewInterceptor.class)
public class ProjectController {
    @Autowired
    UserService userService;
    @Autowired
    ProjectService projectService;

    @Request(value = "/project/edit", format = Request.Format.VIEW)
    public Object edit(String projectId) {
        ProjectEntity project = projectService.selectById(projectId);
        Context.putAttribute("project", project);
        return "dashboard/channel/project/edit";
    }

    @Request(value = "/project/create", format = Request.Format.VIEW)
    public Object create() {
        return "dashboard/channel/project/create";
    }

    @Request(value = "/project/insert", format = Request.Format.JSON)
    public Object insert(@JSON HashMap<String, Object> map) {
        String name = (String) map.get("name");
        String domain = (String) map.get("domain");
        boolean ret = projectService.insert(name,domain);
        Result<String> result = new Result<String>();
        if (!ret) {
            result.setCode(1);
            result.setMsg("操作失败");
        }
        return result;
    }

    @Request(value = "/project/delete", format = Request.Format.JSON)
    public Object delete(String projectId) {
        boolean ret = projectService.delete(projectId);
        return new ErrorResult();
    }

    @Request(value = "/project/update", format = Request.Format.JSON)
    public Object update(@JSON HashMap<String, Object> map) {
        String name = (String) map.get("name");
        String projectId = (String) map.get("projectId");
        String domain = (String) map.get("domain");
        boolean ret = projectService.update(name, projectId,domain);
        Result<String> result = new Result<String>();
        if (!ret) {
            result.setCode(1);
            result.setMsg("操作失败");
        }
        return result;
    }
}
